package com.tech.koufu.ui.activity.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.action.RequestQueueSingleton;
import com.tech.koufu.tools.CookieStringRequest;
import com.tech.koufu.tools.Statics;
import com.tech.koufu.ui.activity.MarketDeatilActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ChangePwdActivity extends Activity{
	private static final String TAG = ChangePwdActivity.class.getName();
	private ImageView img_callback;
	private TextView tv_title;
	private EditText ed_oldpwd,ed_newpwd,ed_newpwdagain;
	private String oldpwd,newpwd,newpwd_again;
	private Button btn_changepwd;
	private CookieStringRequest stringRequest;
	private Context context;
	private MyApplication myApp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_changepwd);

		initView();
	}

	private void initView(){
		myApp = (MyApplication)getApplication();
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_title.setText("修改密码");
		tv_title.setVisibility(View.VISIBLE);
		ed_oldpwd = (EditText)findViewById(R.id.ed_oldpwd);
		ed_newpwd = (EditText)findViewById(R.id.ed_newpwd);
		ed_newpwdagain = (EditText)findViewById(R.id.ed_newpwdagain);

		btn_changepwd = (Button)findViewById(R.id.btn_changepwd);
		btn_changepwd.setOnClickListener(new myOnClickListener());
		img_callback = (ImageView)findViewById(R.id.img_callback);
		img_callback.setVisibility(View.VISIBLE);
		img_callback.setOnClickListener(new myOnClickListener());
	}

	private class myOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.img_callback:
				ChangePwdActivity.this.finish();
				break;
			case R.id.btn_changepwd:
				oldpwd = ed_oldpwd.getText().toString();
				newpwd = ed_newpwd.getText().toString();
				newpwd_again = ed_newpwdagain.getText().toString();

				if(myApp.isLogin){
					if(TextUtils.isEmpty(oldpwd) || TextUtils.isEmpty(newpwd) 
							|| TextUtils.isEmpty(newpwd_again)){
						Toast.makeText(getApplicationContext(), "请先补全信息后再试！", Toast.LENGTH_SHORT).show();
					}else if(oldpwd.equals(newpwd) || oldpwd.equals(newpwd_again)){
						Toast.makeText(getApplicationContext(), "新密码不能与旧密码一致哦！", Toast.LENGTH_SHORT).show();
					}else if(!newpwd.equals(newpwd_again)){
						Toast.makeText(getApplicationContext(), "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
					}else{
						ChangePWD();
					}
				}else{
					Toast.makeText(getApplicationContext(), "请先登录！", Toast.LENGTH_SHORT).show();
				}
				break;
			default:
				break;
			}
		}

	}

	private void ChangePWD(){
		String url = Statics.URL_PHP + Statics.IF_changPWD;
		
		stringRequest = new CookieStringRequest(Method.POST, url, new Listener<String>() {

			@Override
			public void onResponse(String response) {
				// TODO Auto-generated method stub
				if(!TextUtils.isEmpty(response)){
					Log.w("changePWD", response);
					try {
						JSONObject obj = new JSONObject(response);
						if(obj.getInt("status") == 0){
							Toast.makeText(getApplicationContext(), obj.getString("info"), Toast.LENGTH_LONG).show();
							ChangePwdActivity.this.finish();
						}else{
							Toast.makeText(getApplicationContext(), obj.getString("info"), Toast.LENGTH_LONG).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				
			}
			
		}){
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				
				Map<String, String> params = new HashMap<String, String>();
				params.put("userID", myApp.getDigitalid());
				params.put("userName", myApp.getUserName());
				params.put("oldpw",oldpwd);
				params.put("newpw",newpwd);
				
				return params;
			}
		};
		stringRequest.setTag(TAG);
		RequestQueueSingleton.getInstance(context).addToRequestQueue(stringRequest);
	}

}
